package com.ruoyi.chengkao.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.chengkao.domain.CkCandidateAudit;
import com.ruoyi.chengkao.service.impl.CkCandidateAuditServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.chengkao.domain.CkCandidateRegistration;
import com.ruoyi.chengkao.service.ICkCandidateRegistrationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 考生报名信息Controller
 * 
 * @author xiaosong
 * @date 2025-03-17
 */
@Api(tags = "成人高考-考生报名管理")
@RestController
@RequestMapping("/chengkao/CKregistration")
public class CkCandidateRegistrationController extends BaseController
{
    @Autowired
    private ICkCandidateRegistrationService ckCandidateRegistrationService;
    @Autowired
    private CkCandidateAuditServiceImpl ckCandidateAuditServiceImpl;

    /**
     * 查询考生报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('chengkao:CKregistration:list')")
    @GetMapping("/list")
        @ApiOperation("成人高考考生报名信息管理 - 分页查询列表")
    public TableDataInfo list(CkCandidateRegistration ckCandidateRegistration)
    {
        startPage();
        List<CkCandidateRegistration> list = ckCandidateRegistrationService.selectCkCandidateRegistrationList(ckCandidateRegistration);
        return getDataTable(list);
    }

    /**
     * 导出考生报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('chengkao:CKregistration:export')")
    @Log(title = "考生报名信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
        @ApiOperation("成人高考考生报名信息管理 - 导出数据")
    public void export(HttpServletResponse response, CkCandidateRegistration ckCandidateRegistration)
    {
        List<CkCandidateRegistration> list = ckCandidateRegistrationService.selectCkCandidateRegistrationList(ckCandidateRegistration);
        ExcelUtil<CkCandidateRegistration> util = new ExcelUtil<CkCandidateRegistration>(CkCandidateRegistration.class);
        util.exportExcel(response, list, "考生报名信息数据");
    }

    /**
     * 获取考生报名信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('chengkao:CKregistration:query')")
    @GetMapping(value = "/{id}")
        @ApiOperation("成人高考考生报名信息管理 - 根据ID获取详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ckCandidateRegistrationService.selectCkCandidateRegistrationById(id));
    }

    /**
     * 新增考生报名信息
     */
//    @PreAuthorize("@ss.hasPermi('chengkao:CKregistration:add')")
    @Log(title = "考生报名信息", businessType = BusinessType.INSERT)
    @PostMapping
        @ApiOperation("成人高考考生报名信息管理 - 新增数据")
    public AjaxResult add(@RequestBody CkCandidateRegistration ckCandidateRegistration)
    {
        return toAjax(ckCandidateRegistrationService.insertCkCandidateRegistration(ckCandidateRegistration));
    }

    /**
     * 修改考生报名信息
     */
    @PreAuthorize("@ss.hasPermi('chengkao:CKregistration:edit')")
    @Log(title = "考生报名信息", businessType = BusinessType.UPDATE)
    @PutMapping
        @ApiOperation("成人高考考生报名信息管理 - 修改数据")
    public AjaxResult edit(@RequestBody CkCandidateRegistration ckCandidateRegistration)
    {
        return toAjax(ckCandidateRegistrationService.updateCkCandidateRegistration(ckCandidateRegistration));
    }

    //增加根据已有记录决定插入还是更新的upsert接口
//    @PreAuthorize("@ss.hasPermi('chengkao:CKregistration:upsert')")
    @Log(title = "考生报名信息", businessType = BusinessType.OTHER)
    @PostMapping("/upsert")
        @ApiOperation("成人高考考生报名信息管理 - 插入或更新数据")
    public AjaxResult upsert(@RequestBody CkCandidateRegistration ckCandidateRegistration)
    {
        // 插入ck_candidate_audit中一条审核信息
        // 检查audit中是否包含数据

        CkCandidateAudit ckCandidateAudit = new CkCandidateAudit();
        ckCandidateAudit.setCandidateNumber(ckCandidateRegistration.getIdNumber());
        return toAjax(ckCandidateRegistrationService.upsertCkCandidateRegistration(ckCandidateRegistration,ckCandidateAudit));
    }

    /**
     * 删除考生报名信息
     */
    @PreAuthorize("@ss.hasPermi('chengkao:CKregistration:remove')")
    @Log(title = "考生报名信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
        @ApiOperation("成人高考考生报名信息管理 - 删除数据")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ckCandidateRegistrationService.deleteCkCandidateRegistrationByIds(ids));
    }
}
